<?php
namespace app\admin\controller;


use app\admin\logic\BaseLogic;
use think\facade\View;
use app\BaseController;
use think\facade\Request;
use app\admin\model\Banner;

class Base extends BaseController
{
    //修改密码
    public function pass()
    {
        return View::fetch();
    }
    //网站设置
    public function info()
    {
        return View::fetch();
    }
    //首页轮播
    public function adv()
    {
        //查询语句
        $list = Banner::getBanner();
        View::assign('list',$list);
        return View::fetch();
    }
    //首页轮播添加
    public function advAdd()
    {
        //查询banner表排序值
        $list = Banner::column('sort');
        //接值
        $param = Request::param();
        //添加数组
        if (Request::isPost($param)){
            //逻辑层处理添加数据
            $add = BaseLogic::addAdvAttr($param);
            //上传图片
            list($status,$banner) = Upload::getPathsInfo($this->post,'banner');
            if ($status){
                list($path_dir,$path_date) = Upload::imgUpload($banner);
                $add['images'] = $banner;
            }
            list($res,$msg) = Banner::addBannerAttr($add,$list);
            if ($res){
                if ($status && !empty($banner)){
                    //移动临时文件到正式目录下
                    $z_dir = './static/'.$path_date;
                    if (!is_dir($z_dir)){
                        mkdir($z_dir,0777,true);
                    }
                    copy('./static/'.$banner,'./static/'.$path_dir);
                    unlink('./static/'.$banner);
                }
                $this->success('添加成功',url('adv'));
            }else{
                $this->error($msg);
            }
        }
    }
    //首页轮播修改
    public function advEdit()
    {

        if (Request::isPost()){

            //接值
            $post = Request::post();
            //查询banner表排序值
            $list = Banner::where('banner_id','<>',$post['banner_id'])->column('sort');
            //逻辑层处理修改数据
            $edit = BaseLogic::editAdvAttr($post);
            //单图修改
            list($strtic,$banner) = Upload::getPathsI455nfo('banner');
            //单图上传处理
            if ($strtic && !empty($banner)){
                list($path_dir,$path_date) = Upload::imgUpload($banner);
                $edit['images'] = $path_dir;
            }
            $obj = Banner::find($edit['banner_id']);
            $old = $obj->images;
            $res = $obj->save($edit);
            //模型层修改数据库
//            list($res,$msg) = Banner::editBannerAttr($edit,$list);
            if ($res){
                if ($strtic && !empty($banner)){
                    //移动临时文件到正式目录下
                    $z_dir = './static/'.$path_date;
                    if (!is_dir($z_dir)){
                        mkdir($z_dir,0777,true);
                    }
                    copy('./static/'.$banner,'./static/'.$path_dir);
                    unlink('./static/'.$banner);
                }
                if ($strtic && !empty($old) && !empty($image)){
                    unlink('./static/'.$old);
                }
                $this->success('修改成功',url('adv'));
            }else{
                $this->error('修改失败');
            }
        }else{
            //接值
            $get = Request::get();
            $list = Banner::where('banner_id','=',$get['id'])->find()->toArray();
            View::assign('list',$list);
        }

        return View::fetch();
    }
    //首页轮播真删除
    public function advDel()
    {
        //接值
        $get = Request::get();
        list($res,$msg) = Banner::delBannerAttr($get);
        if ($res){
            $this->success($msg,url('adv'));
        }else{
            $this->error($msg);
        }
    }

}